import App from './App'
import store from './store';

const logic = {
	systemInfo: null,
	/**
	 * 显示消息提示
	 * 
	 * @param {Object} text
	 * @param {Object} icon
	 */
	showToast(text, icon) {
		return uni.showToast({
			title: text || '加载中',
			icon: icon || 'none',
			mask: true,
			duration: 2000
		});
	},
	/**
	 * 全局方法 - px2rpx 
	 *
	 * @param {Object} px
	 */
	px2rpx(px) {
		if (this.systemInfo === null) {
			this.systemInfo = uni.getSystemInfoSync()
		}

		//  官方规定屏幕宽为750rpx
		return px / this.systemInfo.windowWidth * 750;
	},

	/**
	 * 全局方法 - rpx2px
	 * 
	 * @param {Object} rpx
	 */
	rpx2px(rpx) {
		if (this.systemInfo === null) {
			this.systemInfo = uni.getSystemInfoSync()
		}

		//  官方规定屏幕宽为750rpx
		return rpx / 750 * this.systemInfo.windowWidth;
	},
}

import {
	createSSRApp
} from 'vue'
export function createApp() {
	const app = createSSRApp(App)
	app.use(store)
	app.config.globalProperties.$logic = logic
	return {
		app
	}
}

function isPromise(obj) {
	return (!!obj &&
		(typeof obj === "object" || typeof obj === "function") &&
		typeof obj.then === "function"
	);
}

// uni方法转then数组
uni.addInterceptor({
	returnValue(res) {
		if (!isPromise(res)) {
			return res;
		}
		const returnValue = [undefined, undefined];
		return res
			.then((res) => {
				returnValue[1] = res;
			})
			.catch((err) => {
				returnValue[0] = err;
			})
			.then(() => returnValue);
	},
});
